package com.stu.module.controller;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @projectName: learn_used_code
 * @packageName: com.stu.module.controller
 * @className: com.stu.module.controller-> TestController
 * @createDate: 2021-12-17 17:52
 * @author: ZhangSheng
 * @version: 1.0
 * @description:
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job dataHandleJob;

    @RequestMapping(value ="/hanldJob",method = RequestMethod.GET)
    public String handlJob(){
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        JobParameters jobParameters = jobParametersBuilder.addLong("time", System.currentTimeMillis()).toJobParameters();
        try {
            jobLauncher.run(dataHandleJob,jobParameters);
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }


}
